package com.hxh.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.hxh.entity.ProjectDatasource;
import com.hxh.entity.input.ProjectDatasourceInput;
import com.hxh.entity.output.ApiResult;
import com.hxh.service.ProjectDatasourceService;
import com.hxh.util.MysqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/projectDatasource")
@Slf4j
public class ProjectDatasourceController {
    @Autowired
    ProjectDatasourceService projectDatasourceService;

    @PostMapping(value = "testDatasource")
    public ApiResult testDatasource(ProjectDatasource projectDatasource) {
        try {
            MysqlUtils.queryOneAndReturnFirstResult(projectDatasource, "select 1;");
            return ApiResult.ok("连接成功");
        } catch (Exception e) {
            log.error("连接失败", e);
            return ApiResult.fail("连接失败！");
        }
    }

    //获取数据源管理分页
    @RequestMapping(value = "/listPage", produces = "application/json;charset=utf-8")
    public ApiResult listPage(ProjectDatasourceInput pojo) {
        Page<ProjectDatasource> page = new Page<>(pojo.getCurrent(), pojo.getPageSize());
        projectDatasourceService.listPage(page, pojo);
        return ApiResult.ok("获取数据源管理分页成功", page);
    }

    @RequestMapping(value = "/listAll", produces = "application/json;charset=utf-8")
    public ApiResult listAll() {
        List<ProjectDatasource> list = projectDatasourceService.listAll();
        return ApiResult.ok("获取所有数据源成功", list);
    }


    //新增数据源管理
    @PostMapping(value = "add")
    public ApiResult add(ProjectDatasource projectDatasource, String sql) {
        try {
            boolean flag = projectDatasourceService.insert(projectDatasource);
            if (flag) {
                MysqlUtils.executeCreateDatabaseSql(projectDatasource, sql);
                return ApiResult.ok("保存成功");
            }
            return ApiResult.fail("保存失败");
        } catch (Exception e) {
            log.error("创建数据源异常", e);
            return ApiResult.fail("创建数据源异常:" + e.getMessage());
        }
    }

    //修改数据源管理
    @PostMapping(value = "update")
    public ApiResult update(ProjectDatasource projectDatasource) {
        boolean flag = projectDatasourceService.update(projectDatasource);
        if (flag) {
            return ApiResult.ok("更新成功");
        } else {
            return ApiResult.fail("更新失败");
        }
    }

    //删除数据源管理
    @PostMapping(value = "delete")
    public ApiResult deleteById(@RequestParam Integer id) {
        boolean flag = projectDatasourceService.deleteById(id);
        if (flag) {
            return ApiResult.ok("删除成功");
        } else {
            return ApiResult.fail("删除失败");
        }
    }

}
